import Toast from "../../../miniprogram_npm/@vant/weapp/toast/toast";
import {
  ajaxOrderApplyRefund
} from '../../../api/order'
const util = require('../../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //自定义导航高度
    customNavHeight: app.globalData.customNavInfo.navHeight,
    baseURL: app.globalData.config.baseURL,
    params: {},
    // 备注
    remarks: ''
  },
  updateValue: util.updateValue,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    app.getPrevPageData(this).then(async (res) => {
      let data = res.data || {};
      this.setData({
        params: data
      })
    })
  },
  async submit() {
    let {
      orderId
    } = this.data.params,
      remarks = this.data.remarks;
    if (!orderId) {
      Toast('没有订单号')
      return;
    }
    if (remarks.length > 200) {
      Toast('填写的原因内容过长')
      return;
    }
    try {
      Toast.loading({
        message: '正在处理',
        forbidClick: true,
        duration: 0
      });

      await ajaxOrderApplyRefund({
        orderId,
        context: remarks
      })
      await app.myNextTick()
      Toast.clear();
      // 修改订单详情页面状态为已申请退款4
      let page = app.getQueryUrlPage('pages/order/orderDetails/index');
      if (page) {
        page.setData({
          'pageData.orderStatus': 4
        })
      }
      Toast({
        type: 'success',
        forbidClick: true,
        message: '操作成功',
        duration: 1000,
        onClose: () => {
          wx.navigateBack({
            delta: 1,
          })
        }
      })
    } catch (err) {
      await app.myNextTick()
      Toast.clear();
      Toast(err.msg || '提交失败');
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  }
})